<template>
  <div class="image-preview__wrapper" v-show="visible">
    <a class="close" href="javascript:;" @click="close">
      <svg-icon icon-class="close"/>
    </a>
    <div class="image-box" :style="styleClass">
      <transition name="fade">
        <div v-if="visible" class="image-box__inner">
          <img :src="imagePreviewUrl">
        </div>
      </transition>
    </div>
  </div>
</template>

<script>
export default {
  name: 'ImagePreview',
  data() {
    return {
      closed: false
    }
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    width: {
      type: [String, Number],
      default: '600px'
    },
    imagePreviewUrl: {
      type: String,
      default: ''
    }
  },
  computed: {
    styleClass() {
      let style = {}
      if (this.width) {
        style.width = this.width
      }
      return style
    }
  },
  methods: {
    close() {
      this.$emit('update:visible', false)
      this.$emit('close')
    }
  }
}
</script>

<style lang="scss" scoped>
  .image-preview__wrapper {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    overflow: auto;
    background: rgba(0, 0, 0, .85);
  }
  .close {
    width: 80px;
    height: 80px;
    background: #42b983;
    display: inline-block;
    border-bottom-left-radius: 80px;
    position: absolute;
    top: 0;
    right: 0;
    color: #ccc;
    font-size: 35px;
    text-align: center;
    line-height: 1.5;
    .svg-icon {
      transition: all .3s;
      transform: rotateZ(0);
    }
    &:hover {
      background: #54c592;
      color: #f5f5f5;
      .svg-icon {
        transform: rotateZ(90deg);
      }
    }
  }
  .image-box {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    img {
      max-width: 100%;
    }
  }
  .fade-enter-active, .fade-leave-active {
    transition: all .2s;
    transform: scale(1);
  }
  .fade-enter, .fade-leave-to {
    transform: scale(0);
  }
</style>
